package cn.ecut.controller;

import cn.ecut.model.Guanliyuan;
import cn.ecut.model.Message;
import cn.ecut.model.Student;
import cn.ecut.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @ResponseBody
    @RequestMapping("/login")
    public Message checkLogin(HttpSession session , String number, String password){
        Message message = new Message();
        String p = studentService.selectPasswordBynumber(number);
        if(password.equals(p)){
            session.setAttribute("number",number);
            session.setAttribute("password",password);
            message.setFlag(true);
        }else {
            message.setFlag(false);
            message.setMsg("用户名或密码错误");
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/changeMima")
    public Message changepassword(HttpSession session,String password1,String password2){
        Message message =new Message();
        String number = (String) session.getAttribute("number");
        if(password1.equals(studentService.selectPasswordBynumber(number))){
            Student student = new Student();
            student.setNumber(number);
            student.setPassword(password2);
            int role = studentService.updatePasswordBynumber(student);
            if(role == 1){
                message.setFlag(true);
                return message;
            }else {
                message.setFlag(false);
                message.setMsg("修改密码失败");
                return message;
            }
        }
        message.setFlag(false);
        message.setMsg("原密码有误");
        return message;
    }

    @ResponseBody
    @RequestMapping("/register")
    public Message register(HttpSession httpSession,Student student){
        Message message = new Message();
        int role =studentService.slectCountByphone(student.getPhone());
        int role2=studentService.selectCountBynumber(student.getNumber());
        if(role!=0){
            message.setFlag(false);
            message.setMsg("该学号已被注册");
        }else if(role2!=0){
            message.setFlag(false);
            message.setMsg("该手机号已被注册");
        } else {
            studentService.addStudent(student);
            message.setFlag(true);
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/showInformation")
    public Student showInformation(HttpSession session){
        Student student = new Student();
        System.out.println((String)session.getAttribute("number"));
        student=studentService.selectInformationBynumber((String)session.getAttribute("number"));
        return student;
    }

    @ResponseBody
    @RequestMapping("/updataStudent")
    public Message updataStudent(Student student){
        Message message = new Message();
        int role = studentService.updataStudent(student);
        if(role ==1){
            message.setFlag(true);
        }else {
            message.setFlag(false);
            message.setMsg("修改失败");
        }
        return message;
    }
}
